package com.dong.rocketmq_demo.producer;

import com.dong.rocketmq_demo.bean.User;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Producer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //发送消息实例
    public void sendMessage(String topic ,String msg){
        rocketMQTemplate.convertAndSend(topic,msg);
    }

    //发送事务消息
    public void sendMessageInTransaction(String topic ,String msg) throws InterruptedException {
        String[] tags = {"TagA", "TagB", "TagC", "TagD", "TagE"};
        for (int i = 0; i < 10; i++) {
            Message<String> message = MessageBuilder.withPayload(msg).build();
            String destination = topic + ":" + tags[i % tags.length];
            TransactionSendResult sendResult =
                    rocketMQTemplate.sendMessageInTransaction(destination, message, destination);
            System.out.printf("%s%n", sendResult);
            Thread.sleep(10);
        }
    }

    /**
     * 发送顺序消息
     * @param topic
     * @param list
     */
    public void sendMessageOrderly(String topic,List<User> list){
        for (User user : list) {
            rocketMQTemplate.syncSendOrderly(topic, MessageBuilder.withPayload(user.toString()).build(), String.valueOf(user.getName()));
        }
    }
}
